package com.changsideal.util;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.Bucket;
import com.changsideal.tool.BucketValidator;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @ClassName BucketUtil
 * @Description bucket basic opera
 * @Author Create by Chansideal.Haosc
 * @Date 2019/6/14 14:47
 * @Version 1.0
 **/
@Slf4j
public final class BucketUtil {

    public static Bucket createBucket(AmazonS3 client, String bucketName) {
        Bucket bucket = null;
        if (BucketValidator.checkBucketInfoExits(client, bucketName)) {
            bucket = client.createBucket(bucketName);
            if (bucket == null) {
                log.warn("创建BUCKET对象失败，bucketName: {}", bucketName);
            } else {
                log.info("创建BUCKET对象成功，bucketName: {}", bucketName);
            }
        }
        return bucket;
    }

    public static Boolean delBucket(AmazonS3 client, String bucketName, Boolean delAll) {
        boolean exits = client.doesBucketExistV2(bucketName);
        if (exits && delAll) {
            client.deleteBucket(bucketName);
            log.warn("删除BUCKET成功，bucketName: {}", bucketName);
            return true;
        }
        log.warn("删除BUCKET失败，bucketName:{}", bucketName);
        return false;
    }

    public static List<Bucket> listBucket(AmazonS3 s3) {
        return s3.listBuckets();
    }
}
